﻿/*
 * 版 本 V1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：数据源	
 */
var refreshGirdData; // 更新数据
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;
                learun.layerForm({
                    id: 'Form',
                    title: '添加数据源',
                    url: config.webportal_server + 'pages/database/source/form.html?access_token=' + config.getToken().access_token,
                    width: 700,
                    height: 430,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'Form',
                        title: '编辑数据源',
                        url: config.webportal_server + 'pages/database/source/form.html?access_token=' + config.getToken().access_token,
                        width: 700,
                        height: 430,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认删除该项！', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'dbSources/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 查看测试
            $('#lr_eye').on('click', function () {
                var id = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(id)) {
                    learun.layerForm({
                        id: 'TestForm',
                        title: '数据源测试查看',
                        url: config.webportal_server + 'pages/database/source/testForm.html?id=' + id + '&access_token=' + config.getToken().access_token,
                        width: 1000,
                        height: 800,
                        maxmin: true,
                        btn: null
                    });
                }
            });


        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'dbSources?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "编码", name: "code", width: 150, align: "left"},
                    {label: "名称", name: "name", width: 150, align: "left"},
                    {
                        label: "数据库", name: "dbId", width: 200, align: "left",
                        // formatterAsync: function (callback, value, row) {
                        //     learun.clientdata.getAsync('db', {
                        //         key: value,
                        //         callback: function (item) {
                        //             callback(item.alias + '(' + item.name + ')');
                        //         }
                        //     });
                        // }
                    },
                    {
                        label: '创建时间', name: 'createTime', width: 130, align: 'left',
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm');
                        }
                    },
                    {label: "备注", name: "description", width: 300, align: "left"},
                ],
                dblclick: function (row) {
                    selectedRow = $('#gridtable').jfGridGet('rowdata');
                    var keyValue = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'Form',
                            title: '编辑数据源',
                            url: config.webportal_server + 'pages/database/source/form.html?access_token=' + config.getToken().access_token,
                            width: 700,
                            height: 430,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                mainId: 'id',
                reloadSelected: true,
                isPage: true
            });
            page.search();
        },
        search: function (param) {
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


